package com.mayuanfei.springboot06.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.mayuanfei.springboot06.domain.entity.Bank;
import com.mayuanfei.springboot06.domain.entity.MisLog;
import com.mayuanfei.springboot06.domain.entity.SysUser;
import com.mayuanfei.springboot06.domain.mapper.BankMapper;
import com.mayuanfei.springboot06.domain.mapper.MisLogMapper;
import com.mayuanfei.springboot06.domain.mapper.SysUserMapper;
import com.mayuanfei.springboot06.service.TestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * TODO 类描述
 *
 * @author 老马
 * @date 2023-06-02 16:56
 */
@Service
public class TestServiceImpl implements TestService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private MisLogMapper misLogMapper;
    @Resource
    private BankMapper bankMapper;

    /**
     * 测试多数据源查询
     */
    @Override
    public Map<String, Object> testSelect() {
        Map<String, Object> result = MapUtil.newHashMap();
        // mysql数据源查询
        SysUser sysUser = sysUserMapper.selectById(3L);
        result.put("userId", sysUser.getUserId());
        result.put("userName", sysUser.getUserName());
        // oracel18数据源查询
        Bank bank = new LambdaQueryChainWrapper<>(this.bankMapper)
                .eq(Bank::getBankid, 777L)
                .one();
        result.put("bankName", bank.getBankname());
        // oracel183数据源查询
        MisLog misLog = this.misLogMapper.selectById("1664560539193565184");
        result.put("operateDate", misLog.getOperation());
        return result;
    }

    @Override
    @DSTransactional
    public void testRollback() {
        //mysql 插入
        SysUser sysUser = new SysUser();
        sysUser.setUserName("老马77");
        sysUser.setNickName("老马哥77");
        this.sysUserMapper.insert(sysUser);
        System.out.println(sysUser);
        //18数据库 插入
        Bank bank = new Bank();
        bank.setBankid("777");
        bank.setBankname("测试银行777");
        this.bankMapper.insert(bank);
        //183数据库 插入
        MisLog misLog = new MisLog();
        misLog.setId(IdUtil.getSnowflakeNextIdStr());
        misLog.setOperation("测试插入777");
        this.misLogMapper.insert(misLog);
        int i = 1/0;
    }
}